package com.example.webservice.service.impl;

import com.example.webservice.service.IKpiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

//
@Service
@Slf4j
public class IKpiServiceImpl implements IKpiService {
//
////    @Autowired
////    ISysUserService userService;
////
////    @Autowired
////    ISysLoginLogService loginLogService;
////
////    @Autowired
////    IJobService jobService;
////
////    @Autowired
////    IManageAuthService manageAuthService;
//
////    @Value("${spring.datasource.url}")
////    String url;
////
////    @Value("${spring.datasource.username}")
////    String username;
////
////    @Value("${spring.datasource.password}")
////    String password;
//
//    @Override
//    public String getKpiValue(String api) {
//        return "";
////        String res = null;
////        switch (api) {
////            case "BusinessSystemOnlineNum":
////                res = getBusinessSystemOnlineNum();
////                break;
////            case "BusinessUserRegNum":
////                res = getBusinessUserRegNum();
////                break;
////            case "BusinessSystemSessionNum":
////                res = getBusinessSystemSessionNum();
////                break;
////            case "BusinessVisitCount":
////                res = getBusinessVisitCount();
////                break;
////            case "BusinessDataTableSpace":
////                res = getBusinessDataTableSpace();
////                break;
////            case "BusinessDayLoginNum":
////                res = getBusinessDayLoginNum();
////                break;
////            case "BusinessSystemResponseTime":
////                res = getBusinessSystemResponseTime();
////                break;
////            case "BusinessSystemRunningTime":
////                res = getBusinessSystemRunningTime();
////                break;
////            case "BusinessSystemDBTime":
////                res = getBusinessSystemDBTime();
////                break;
////        }
////
////        return res;
//    }
//
//    /**
//     * 1.在线用户数
//     **/
//    public String getBusinessSystemOnlineNum() {
//        List<HttpSession> httpses = new HttpSessionConfigUtil().getActiveSessions();
//        int num=0;
//        for (HttpSession session : httpses) {
//            SysUser user = (SysUser) session.getAttribute("user");
//            log.info("user={}",user);
//            if(user!=null){
//                num++;
//            }
//        }
//        return String.valueOf(num);
//    }
//
//    /**
//     * 2.注册用户数
//     **/
//    public String getBusinessUserRegNum() {
//        Paging users = manageAuthService.getUsers();
//        int count = users.getCount();
//        return String.valueOf(count);
//    }
//
//    /**
//     * 3.页面会话连接数
//     **/
//    public String getBusinessSystemSessionNum() {
//        List<HttpSession> httpses = new HttpSessionConfigUtil().getActiveSessions();
//        int num=0;
//        for (HttpSession session : httpses) {
//            SysUser user = (SysUser) session.getAttribute("user");
//            log.info("user={}",user);
//            if(user!=null){
//                num++;
//            }
//        }
//        return String.valueOf(num);
//    }
//
//    /**
//     * 4.累计访问人次
//     **/
//    public String getBusinessVisitCount() {
//        Page<SysLoginLog> pages = new Page<>(0, 10);
//        List<SysLoginLog> sysLoginLogs = loginLogService.queryLoginLogPage(pages, "", "", "", "", "", "loginTime", "asc");
//        long total = pages.getTotal();
//        return String.valueOf(total);
//    }
//
//    /**
//     * 5.业务数据占用表空间大小
//     **/
//    public String getBusinessDataTableSpace() {
//        //得到数据库名称
//        String database = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("?"));
//        long size = 0;
//        List<String> tableList = Arrays.asList("item_catalog", "certificate_catalog", "certificate_data",
//                "certificate_template", "interface_log","sys_user");
//        //循环查询业务表大小
//        for (String table : tableList) {
//            size += jobService.queryTableSize(database, table);
//        }
//        //格式化数据，得到MB类型
//        double sizeMB = size / 1024d / 1024d;
//        String res = String.format("%.2f", sizeMB);
//        return res;
//    }
//
//    /**
//     * 6.日登陆人数
//     **/
//    public String getBusinessDayLoginNum() {
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String loginTime = sdf.format(new Date());
//        List<SysLoginLog> logList = loginLogService.queryLoginLog("1", loginTime, "");
//        Set<String> userSet=new HashSet<>();
//        for (SysLoginLog loginLog : logList) {
//            userSet.add(loginLog.getLoginName());
//        }
//        return String.valueOf(userSet.size());
//    }
//
//    /**
//     * 7.系统服务平均响应时长
//     **/
//    public String getBusinessSystemResponseTime() {
//        Page<SysLoginLog> pages = new Page<>(0, 1);
//        List<SysLoginLog> retList = loginLogService.queryLoginLogPage(pages, "", "", "", "", "", "loginTime", "desc");
//        SysLoginLog sysLoginLog = retList.get(0);
//        Long loginUseTime = sysLoginLog.getLoginUseTime();
//        return String.valueOf(loginUseTime);
//    }
//
//    /**
//     * 8.系统健康运行时长
//     **/
//    public String getBusinessSystemRunningTime() {
//        long startTime= (long) EhcacheUtils.get("startTime");
//
//        long localTime=System.currentTimeMillis();
//        long healthTime=localTime-startTime;
//        long seconds=healthTime/1000;
//
////        SecurityAuditLog securityAuditLog = new SecurityAuditLog();
////        QueryWrapper<SecurityAuditLog> wrapper = new QueryWrapper<>();
////        //选择系统错误类型日志
////        wrapper.eq("auditType", "4");
////        wrapper.orderByDesc("operTime");
////        wrapper.last("limit 1");
////        SecurityAuditLog auditLog = securityAuditLog.selectOne(wrapper);
////
////        //系统上一次错误时间
////        LocalDateTime operTime = auditLog.getOperTime();
////        //当前时间
////        LocalDateTime now = LocalDateTime.now();
//
//        //计算时间差，秒
////        Duration duration = Duration.between(operTime, now);
////        long seconds = duration.getSeconds();
//
//
//        return String.valueOf(seconds);
//    }
//
//    /**
//     * 9.数据库平均响应时长
//     **/
//    public String getBusinessSystemDBTime() {
//        long begin = 0;
//        try {
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            begin = System.currentTimeMillis();
//            Connection conn = DriverManager.getConnection(url, username, password);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        long end = System.currentTimeMillis();
//        long time = end - begin;
//
//        return String.valueOf(time);
//    }
}
